//
//  CMDBPublishPicsView.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/18.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let KpicViewWH : CGFloat = 150 * CMDB_Rate

private let SelectedPicture = "SelectedPicture"

class CMDBPublishPicsView: BaseCollectionView {

    var images : [UIImage] = [UIImage](){
        didSet{
            reloadData()
        }
    }
    
    override func commitInitOwnProperties() {
        super.commitInitOwnProperties()
        
        backgroundColor = UIColor.whiteColor()
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumInteritemSpacing = KleftMargin
        layout.minimumLineSpacing = KleftMargin
        
        registerClass(CMDBPicPickerViewCell.self, forCellWithReuseIdentifier: SelectedPicture)
    }
}

extension CMDBPublishPicsView{

    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSize(width: KpicViewWH , height: KpicViewWH + 15)
    }
    
    override internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(15, 15, 15, 15)
    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        printLog("collection click")
    }
    
}

extension CMDBPublishPicsView
{
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        printLog(images.count + 1)
        
        return (images.count + 1)
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SelectedPicture, forIndexPath: indexPath) as! CMDBPicPickerViewCell
        
        cell.mDelegate = self
        
        cell.image = indexPath.item <= images.count - 1 ? images[indexPath.item] : nil
        
        return cell
    }
    
}


extension CMDBPublishPicsView{
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        mDelegate?.onItemClickType!("AddPhotoClick", data: nil, indexPath: nil)
    }
    
}